package cn.edu.scau.cmi.ema.domain.base;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

//经过测试，命名查询必须是entity,否则不能被JPA读取。
@Entity
@NamedQueries({
	@NamedQuery(name = "findStandardsByFeatureId", query = "select myFeaturestandard.standard from Featurestandard myFeaturestandard where myFeaturestandard.feature.id = ?1"),
	@NamedQuery(name = "findFeaturesByStandardId", query = "select myFeaturestandard.feature.name,myFeaturestandard.min,myFeaturestandard.max from Featurestandard myFeaturestandard where myFeaturestandard.standard.id = ?1"),
	@NamedQuery(name = "findAddressNameById", query = "select myAddress.name from Address myAddress where myAddress.id=?1"),
	@NamedQuery(name = "findAddressByLevelAndParent", query = "select myAddress from Address myAddress where myAddress.level=?1 and myAddress.parent.id=?2"),
	
	@NamedQuery(name = "findEventsByIdDesc", query = "select myEvent from Event myEvent order by myEvent.id desc"),
	@NamedQuery(name = "findEventIdsByEventLimit", query = "select myEvent.id from Event myEvent where myEvent.id>=?1"),
	@NamedQuery(name = "findFoodbatchsByEventLimit", query = "select myEvent.foodbatch from Event myEvent where myEvent.id>=?1"),
	@NamedQuery(name = "findEventIdAndFoodbatchByLimit", query = "select event.id,event.foodbatch from Event event"),
	@NamedQuery(name = "findEventIdAndFoodbatchByEventNameLimit", query = "select event.id,event.foodbatch from Event event where event.name like concat('%',?1,'%')"),
	@NamedQuery(name = "findEventIdByLimit", query = "select event.id from Event event"),
//	@NamedQuery(name = "findFoodtypeIdsByParent", query = "select foodtype.id from Foodtype foodtype where foodtype.parent.id = ?1"),
	@NamedQuery(name = "findEventByLimit", query = "select event from Event event"),
	@NamedQuery(name = "findPicturesByIdDesc", query = "select myPicture from Picture myPicture order by myPicture.id desc"),
	@NamedQuery(name = "countAddresssByParent", query = "select count(myAddress) from Address myAddress where myAddress.parent.id = ?1"),
	@NamedQuery(name = "countEventsByParent", query = "select count(myEvent) from Event myEvent where myEvent.parent.id = ?1"),
	@NamedQuery(name = "countFoodtypesByParent", query = "select count(myFoodtype) from Foodtype myFoodtype where myFoodtype.parent.id = ?1"),
	@NamedQuery(name = "findAllEventsDesc", query = "select myEvent from Event myEvent order by myEvent.id desc"),
//	@NamedQuery(name = "findRuledetailsByRuleAndSheet", query = "select myRuledetail from Ruledetail myRuledetail where myRuledetail.rule.id = ?1 and myRuledetail.sheet = ?2"),
	@NamedQuery(name = "findEventsByFoodtypeId", query = "select myEvent from Event myEvent where myEvent.foodbatch.food.foodtype.id in (?1)"),
	@NamedQuery(name = "countEventsByFoodtypeId", query = "select count(myEvent.id) from Event myEvent where myEvent.foodbatch.food.foodtype.id in (?1)"),
	@NamedQuery(name = "findMaxDiscoveryTimeInEventByDiscoveryAddress", query = "select substring(cast(max(myEvent.discoveryTime) as string), 1, 7) from Event myEvent where myEvent.discoveryAddress.id in (?1)"),
	@NamedQuery(name = "findMinDiscoveryTimeInEventByDiscoveryAddress", query = "select substring(cast(min(myEvent.discoveryTime) as string), 1, 7) from Event myEvent where myEvent.discoveryAddress.id in (?1)"),
	@NamedQuery(name = "countEventsByDiscoveryAddress", query = "select count(myEvent.id) from Event myEvent where myEvent.discoveryAddress.id = ?1"),// 根据discoveryAddress.id
	@NamedQuery(name = "countEventsByDiscoveryAddressAndfoodtypeIds", query = "select count(myEvent.id) from Event myEvent where myEvent.discoveryAddress.id = ?1 and myEvent.foodbatch.food.foodtype.id in (?2)"),
	@NamedQuery(name = "countEventsByDiscoveryAddressAndDiscoveryTime", query = "select count(myEvent.id) from Event myEvent where myEvent.discoveryAddress.id = ?1 and cast(myEvent.discoveryTime as string) like concat(?2,'%')"),// 根据discoveryAddress.id和discoveryTime(精确到月份，模糊查询)
	@NamedQuery(name = "countEventsByDiscoveryAddressAndDiscoveryTimeAndfoodtypeIds", query = "select count(myEvent.id) from Event myEvent where myEvent.discoveryAddress.id = ?1 and cast(myEvent.discoveryTime as string) like concat(?2,'%') and myEvent.foodbatch.food.foodtype.id in (?3)"),

	//	从echart表中查找
	@NamedQuery(name = "countEcharteventsByDiscoveryaddressAndDiscoverytime", query = "select count(myEchart.id) from Echart myEchart where myEchart.discoveryprovince = ?1 and cast(myEchart.discoveryTime as string) like concat(?2,'%')"),// 根据discoveryAddress.id和discoveryTime(精确到月份，模糊查询)
	@NamedQuery(name = "countEcharteventsByFoodtypeAndDiscoverytime", query = "select count(myEchart.id) from Echart myEchart where myEchart.foodtypename = ?1 and cast(myEchart.discoveryTime as string) like concat(?2,'%')"),// 根据discoveryAddress.id和discoveryTime(精确到月份，模糊查询)

	@NamedQuery(name = "countEcharteventsByDiscoveryaddressAndDiscoverytimeAndFoodtypeids", query = "select count(myEvent.id) from Event myEvent where myEvent.discoveryAddress.id = ?1 and cast(myEvent.discoveryTime as string) like concat(?2,'%') and myEvent.foodbatch.food.foodtype.id in (?3)"),
	
	
	
	
	
	@NamedQuery(name = "findPicturesWithPictureByEventStatusAndFoodtypeIdsDesc", query = "select myPicture from Picture myPicture where myPicture.path not like 'snapshot_%' and myPicture.event.status in (?1) and myPicture.food.foodtype.id in (?2) order by myPicture.id desc"),
	
	@NamedQuery(name = "findFoodsByNameAndProducer", query = "select myFood from Food myFood where myFood.name = ?1 and myFood.producer.name = ?2"),
	
	@NamedQuery(name = "findFoodRelativEvents", query = "select myEvent from Event myEvent where myEvent.foodbatch.food.name = ?1 order by myEvent.id desc"),
	@NamedQuery(name = "findFoodtypeRelativEvents", query = "select myEvent from Event myEvent where myEvent.foodbatch.food.foodtype = ?1 order by myEvent.id desc"),
	@NamedQuery(name = "findDiscoveryadddressRelativEvents", query = "select myEvent from Event myEvent where myEvent.discoveryAddress = ?1 order by myEvent.id desc"),
	@NamedQuery(name = "findSourceAddressRelativEvents", query = "select myEvent from Event myEvent where myEvent.sourceAddress = ?1 order by myEvent.id desc"),
	
	
	@NamedQuery(name = "findEventByFoodname", query = "select myEvent from Event myEvent where myEvent.foodbatch.food = ?1 "),
	
	
	
	
	@NamedQuery(name = "findEventsWithPictureByFoodtypeIdsDesc", query = "select myEvent from Event myEvent where myEvent.foodbatch.food.foodtype.id in (?1) and size(myEvent.picturesForEvent) > 0 order by myEvent.id desc"),
	@NamedQuery(name = "findDetectCrawlerfileByFiletypeAndImporteventstatus", query = "select myCrawlerfile from Crawlerfile myCrawlerfile where myCrawlerfile.filetype.id = ?1 and myCrawlerfile.crawlerfilestatus = ?2"),
	@NamedQuery(name = "findCrawlerdetect4BatchImportDetects", query = "select myCrawlerdetect from Crawlerdetect myCrawlerdetect where myCrawlerdetect.crawlerdetectstatus.id = ?1 "),
	@NamedQuery(name = "findCrawlerevent4BatchImportEvents", query = "select myCrawlerevent from Crawlerevent myCrawlerevent where myCrawlerevent.crawlerdetectstatus.id = ?1 "),
	@NamedQuery(name = "findDetectCrawlerfileByNullExistStatus", query = "select myCrawlerfile from Crawlerfile myCrawlerfile where myCrawlerfile.exist = null "),
	
	@NamedQuery(name = "findCrawlerfileByFiletypeExistCrawlerfilestatus", query = "select myCrawlerfile from Crawlerfile myCrawlerfile where myCrawlerfile.filetype.id = ?1 and myCrawlerfile.exist = ?2 and myCrawlerfile.crawlerfilestatus.id = ?3 "),
	@NamedQuery(name = "findLastEvent", query = "select myEvent from Event myEvent where myEvent.id =  (select max(id) from Event myEvent)"),
	@NamedQuery(name = "findLastEchart", query = "select myEchart from Echart myEchart where myEchart.id = (select max(id) from Echart myEchart)"),
	
	

	
	
	

})// left join myEvent.foodbatch left join myEvent.foodbatch.food left join myEvent.foodbatch.food.foodtype

public class CmiNamedQuery {

//	按照JPA要求，实体类必须要有主键标识
	@Id
	Integer id;

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {

		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}
